package com.zengdb.jdbc.dao.impl;

import java.util.List;

import com.zengdb.jdbc.dao.IStudentDAO;
import com.zengdb.jdbc.domain.Student;
import com.zengdb.jdbc.handler.BeanHandler;
import com.zengdb.jdbc.handler.BeanListHandler;
import com.zengdb.jdbc.util.JdbcTemplate;

public class StudentDAOImpl implements IStudentDAO {

	@Override
	public void save(Student stu) {
		String sql = "INSERT INTO t_student(name,age) VALUES(?,?)";
		Object[] obj = new Object[]{stu.getName(),stu.getAge()};
		JdbcTemplate.update(sql, obj);
	}

	@Override
	public void delete(Long id) {
		String sql = "DELETE FROM t_student WHERE id = ?";
		JdbcTemplate.update(sql, id);
	}

	@Override
	public void modify(Student stu) {
		String sql = "UPDATE t_student SET name = ?, age = ? WHERE id = ?";
		Object[] obj = {stu.getName(),stu.getAge(),stu.getId()};
		JdbcTemplate.update(sql, obj);
	}

	@Override
	public Student queryOne(Long id) {
		String sql = "SELECT id, name, age FROM t_student WHERE id = ?";
		return JdbcTemplate.query(sql, new BeanHandler<>(Student.class), id);
	}

	@Override
	public List<Student> queryList() {
		String sql = "SELECT id, name, age FROM t_student";
		return JdbcTemplate.query(sql, new BeanListHandler<>(Student.class));
	}

}
